package com.audi.adapter;

import java.util.ArrayList;

import com.audi.R;


import com.audi.activity.NewDeviceActivity;
import com.audi.dto.DeviceItem;
import com.audi.dto.PlaceItem;
import com.audi.utils.Constants;
import com.audi.utils.DBHelper;
import com.audi.view.AudiDialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BaseEditDeviceDialog {
	private ArrayList<PlaceItem> mListPlaceCard;
	private SQLiteAsusNewHelper mSQLHelper;
	Context context;
	public BaseEditDeviceDialog(Context activity) {
		this.context = activity;
		mSQLHelper=new SQLiteAsusNewHelper(activity);
	}
	
	public void editNameDevice(String deviceid) {

		final Dialog diaEditName;
		diaEditName = new Dialog(context);
		diaEditName.requestWindowFeature(Window.FEATURE_NO_TITLE);
		diaEditName.setContentView(R.layout.rename_device_dialog);
		// TextView cam = (TextView) diaEditName.findViewById(R.id.text_camera);
		final EditText renameEditText = (EditText) diaEditName
				.findViewById(R.id.edt_rename_device_dilog_input);

		Button cancel = (Button) diaEditName
				.findViewById(R.id.btn_rename_device_dialog_skip);
		Button ok = (Button) diaEditName
				.findViewById(R.id.btn_rename_device_dialog_ok);
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				diaEditName.dismiss();
			}
		});
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// DBHelper dbHelper = new DBHelper();

				SQLiteAsusNewHelper mSQLHelper = new SQLiteAsusNewHelper(
						context);
				// dbHelper.addDataTest(mSQLHelper);
				DeviceItem item = new DeviceItem();
				item.setDeviceId(6);
				item.setDeviceName(renameEditText.getText().toString());
				mSQLHelper.renameDevice(item);
				int w;
				w = mSQLHelper.renameDevice(item);
				if (w == 1) {
					diaEditName.dismiss();
				}

			}
		});

		diaEditName.show();
	}
	public void removeDevice() {

		final Dialog diaRemove;
		diaRemove = new Dialog(context);
		diaRemove.requestWindowFeature(Window.FEATURE_NO_TITLE);
		diaRemove.setContentView(R.layout.remove_card_dialog);
		// TextView cam = (TextView) diaEditName.findViewById(R.id.text_camera);

		Button cancel = (Button) diaRemove
				.findViewById(R.id.btn_remove_cancel);
		Button ok = (Button) diaRemove
				.findViewById(R.id.btn_remove_yes);
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				diaRemove.dismiss();
			}
		});
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// DBHelper dbHelper = new DBHelper();

				SQLiteAsusNewHelper mSQLHelper = new SQLiteAsusNewHelper(
						context);
				// dbHelper.addDataTest(mSQLHelper);
				DeviceItem item = new DeviceItem();
				item.setDeviceId(1);
				//item.setDeviceName(renameEditText.getText().toString());
				mSQLHelper.removeDevice(item);
				int www;
				www = mSQLHelper.removeDevice(item);
				if (www == 1) {
					diaRemove.dismiss();
				}

			}
		});

		diaRemove.show();
	}
	// 3-1-2-1  device connectionSelect a place to edit
		public void showSelectPlaceDialog() {
			final AudiDialog selectPlaceDevice = new AudiDialog(
					context);
			selectPlaceDevice.setLayout(R.layout.select_place_dialog);
			selectPlaceDevice.show();
			// load data placecard
			mListPlaceCard = mSQLHelper.getListPlace();
			ListView placeList = (ListView) selectPlaceDevice
					.findViewById(R.id.list_place_dialog);
			ListPlaceDialogAdapter placeAdapter = new ListPlaceDialogAdapter(
					(Activity)context, mListPlaceCard);
			placeList.setAdapter(placeAdapter);
			LinearLayout llSelectNewPlaceDialog = (LinearLayout) selectPlaceDevice
					.findViewById(R.id.ll_select_new_place_dialog);
			Button btnSkip = (Button) selectPlaceDevice
					.findViewById(R.id.btn_cancle_dialog);
			btnSkip.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					selectPlaceDevice.dismiss();
				}
			});
			llSelectNewPlaceDialog.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					showSelectStatusPlace();
					selectPlaceDevice.dismiss();
				}
			});
			placeList.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
						long arg3) {
						mListPlaceCard.get(pos).getPlaceName();
//						Toast.makeText(context,mListPlaceCard.get(pos).getPlaceName()
//								, Toast.LENGTH_SHORT).show();
						mSQLHelper.getPlaceId(mListPlaceCard.get(pos));
						//Toast.makeText(context,mSQLHelper.getPlaceId(mListPlaceCard.get(pos)), Toast.LENGTH_SHORT).show();
						DeviceItem item = new DeviceItem();
						item.setDeviceId(6);
						item.setDevicePlaceId(mSQLHelper.getPlaceId(mListPlaceCard.get(pos)));
						//item.setDeviceName(renameEditText.getText().toString());
						Toast.makeText(context,mSQLHelper.getPlaceId(mListPlaceCard.get(pos))
								, Toast.LENGTH_SHORT).show();
						
						int w;
						w = mSQLHelper.editPlaceDevice(item);
						if (w == 1) {
							selectPlaceDevice.dismiss();
						}
				}
			});
		}

		// 3-1-2-2 Select public/privateTap
		public void showSelectStatusPlace() {
			final AudiDialog selectStatusPlace = new AudiDialog(
					context);
			selectStatusPlace.setLayout(R.layout.select_capability);
			selectStatusPlace.show();
			TextView txtPrivate = (TextView) selectStatusPlace
					.findViewById(R.id.text_private);
			TextView txtPublic = (TextView) selectStatusPlace
					.findViewById(R.id.text_public);
			txtPrivate.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					showNewPlaceDialog(Constants.PLACE_CAPABILITY.PRIVATE.ordinal());
					selectStatusPlace.dismiss();
				}
			});
			txtPublic.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					showNewPlaceDialog(Constants.PLACE_CAPABILITY.PUBLIC.ordinal());
					selectStatusPlace.dismiss();
				}
			});
		}

		// 3-1-2-3 Create a new place
		public void showNewPlaceDialog(final int cap) {
			final AudiDialog newPlaceDialog = new AudiDialog(context);
			newPlaceDialog.setLayout(R.layout.new_device_dialog);
			newPlaceDialog.show();
			TextView createNewPlaceTitle = (TextView) newPlaceDialog
					.findViewById(R.id.txt_new_device_dilog_title);
			TextView createNewPlaceHint = (TextView) newPlaceDialog
					.findViewById(R.id.txt_new_device_dilog_hint);
			final EditText createNewPlaceEdit = (EditText) newPlaceDialog
					.findViewById(R.id.edit_new_device_dialog_input);
			Button cancleBtn = (Button) newPlaceDialog
					.findViewById(R.id.btn_new_device_dialog_skip);
			Button okBtn = (Button) newPlaceDialog
					.findViewById(R.id.btn_new_device_dialog_next);
			createNewPlaceEdit.setHint(R.string.create_place_hint);
			createNewPlaceEdit.setText("");
			createNewPlaceTitle.setText(R.string.create_place_title);
			createNewPlaceHint.setVisibility(View.GONE);
			cancleBtn.setText(R.string.cancel);
			okBtn.setText(R.string.ok);
			okBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					newPlaceDialog.dismiss();
					final PlaceItem itemp1 = new PlaceItem();
					//itemp1.setPlaceId(0);
					itemp1.setPlaceImage("");
					itemp1.setPlaceName(createNewPlaceEdit.getText().toString());
					itemp1.setCapabilityType(cap);
					
					if (mSQLHelper.checkExistPlace(itemp1)) {
						showNoticePlace();
					}
					mSQLHelper.addPlace(itemp1);
					DeviceItem item = new DeviceItem();
					item.setDeviceId(6);
					item.setDevicePlaceId(4);
					mSQLHelper.editPlaceDevice(item);
					//showNoticePlace();
				}
			});
			cancleBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					newPlaceDialog.dismiss();
				}
			});
		}

		// 3-1-2-4 Notice Dialog
		public void showNoticePlace() {
			final AudiDialog noticeDialog = new AudiDialog(context);
			noticeDialog.setLayout(R.layout.notice_new_place_dialog);
			noticeDialog.show();
		}
}
